package com.zhoug.zmui3demo.ui;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.zhoug.common3.adapters.recycler2.BaseRecyclerViewAdapter;
import com.zhoug.common3.adapters.recycler2.BaseViewHolder;
import com.zhoug.common3.utils.LogUtils;
import com.zhoug.common3.view.edit.ZMUIEditText;
import com.zhoug.common3.view.edit.ZMUITextWatcher;
import com.zhoug.zmui3.ZMUINumberStepView;
import com.zhoug.zmui3demo.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 步进器
 *
 * @Author: zhoug
 * @Date: 2024-07-15
 * @Description:
 */
public class StepNumberActivity extends AppCompatActivity {
    private static final String TAG = ">>>StepNumberActivity";
    private Adapter mAdapter;
    private Switch switchEnable;
    private Switch switchError;
    private Switch switchEditEnabled;
    private Switch switchDecimal;
    private Switch switchSigned;
    private Switch switchValidate;
    private Switch switchLoop;
    private ZMUINumberStepView numberStepView1;
    private Button btnSubmit;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_number);
        findViews();
//        numberStepView
        switchEnable.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                numberStepView1.setEnabled(!isChecked);
                mAdapter.setEnable(!isChecked);
                mAdapter.notifyDataSetChanged();
            }
        });
        switchError.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                numberStepView1.setIsError(isChecked);
                mAdapter.setError(isChecked);
                mAdapter.notifyDataSetChanged();

            }
        });

        switchEditEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                numberStepView1.setEditEnabled(isChecked);
                mAdapter.setEditEnabled(isChecked);
                mAdapter.notifyDataSetChanged();
            }
        });

        switchValidate.setOnCheckedChangeListener((buttonView, isChecked) -> {
            numberStepView1.setValidateOutFocus(isChecked);
            mAdapter.setValidate(isChecked);
            mAdapter.notifyDataSetChanged();
        });
        switchDecimal.setOnCheckedChangeListener((buttonView, isChecked) -> {
            numberStepView1.setIsDecimal(isChecked);
            mAdapter.setDecimal(isChecked);
            mAdapter.notifyDataSetChanged();
        });

        switchSigned.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(isChecked){
                numberStepView1.setMinMaxNumber(-10,20);
                mAdapter.setMinMax(-10,20);
            }else{
                numberStepView1.setMinMaxNumber(0,20);
                mAdapter.setMinMax(0,20);
            }
            mAdapter.notifyDataSetChanged();
        });

        switchLoop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                numberStepView1.setLoopMinMax(isChecked);
                mAdapter.setLoop(isChecked);
                mAdapter.notifyDataSetChanged();
            }
        });

        numberStepView1.setMinMaxNumber(5,23);

        mAdapter=new Adapter();
        mAdapter.setMinMax(5,23);
        mAdapter.setData(getData());
        recyclerView.setAdapter(mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        btnSubmit.setOnClickListener(v->{
            List<Product> data1 = mAdapter.getData();
            for (Product item : data1) {
                LogUtils.d(TAG, item.getName()+":price:"+item.getPrice()+",remark:"+item.getRemark());
            }
        });
    }

    private List<Product> getData(){
        List<Product> data=new ArrayList<>();
        for (int i = 1; i <= 60; i++) {
            Product product = new Product();
            product.setName("商品"+i);
            data.add(product);
        }
        return data;
    }



    private void findViews() {
        switchEnable = findViewById(R.id.switch_enable);
        switchError = findViewById(R.id.switch_error);
        switchEditEnabled = findViewById(R.id.switch_editEnabled);
        switchDecimal = findViewById(R.id.switch_decimal);
        switchSigned = findViewById(R.id.switch_signed);
        switchValidate = findViewById(R.id.switch_validate);
        switchLoop = findViewById(R.id.switch_loop);
        numberStepView1 = findViewById(R.id.numberStepView1);
        btnSubmit = findViewById(R.id.btn_submit);
        recyclerView = findViewById(R.id.recyclerView);
    }

    protected interface Function<T> {
        void apply(T t);
    }


    private  class Adapter extends BaseRecyclerViewAdapter<Product> {
        private boolean enable=true;
        private boolean error=false;
        private boolean editEnabled=true;
        private boolean loop=true;
        private boolean validate;
        private boolean isDecimal;
        private int min;
        private int max;

        public void setEnable(boolean enable) {
            this.enable = enable;
        }

        public void setError(boolean error) {
            this.error = error;
        }

        public void setLoop(boolean loop) {
            this.loop = loop;
        }

        public void setEditEnabled(boolean editEnabled) {
            this.editEnabled = editEnabled;
        }

        public void setValidate(boolean validate) {
            this.validate = validate;
        }

        public void setDecimal(boolean decimal) {
            isDecimal = decimal;
        }

        public void setMinMax(int min,int max) {
            this.min = min;
            this.max = max;
        }



        @Override
        protected int getLayoutId(int viewType) {
            return R.layout.test_step_number_list_item;
        }

        @Override
        protected void onBindData(BaseViewHolder holder, Product data, int position, int viewType) {
            if(data!=null){
                TextView tvProductName=holder.getView(R.id.tv_productName);
                ZMUINumberStepView numberStepView=holder.getView(R.id.numberStepView_price);
                ZMUIEditText etRemark=holder.getView(R.id.et_remark);
                tvProductName.setText(data.getName());
                numberStepView.setMinMaxNumber(min,max);
                numberStepView.setNumber(data.getPrice(), false);
                numberStepView.setTag(position);
                numberStepView.setOnNumberChangeListener(onNumberChangeListener);
                numberStepView.setEnabled(enable);
                numberStepView.setIsError(error);
                numberStepView.setLoopMinMax(loop);
                numberStepView.setEditEnabled(editEnabled);
                numberStepView.setValidateOutFocus(validate);
                numberStepView.setIsDecimal(isDecimal);
                etRemark.setTag(position);
                etRemark.removeTextChangedListener(textWatcher);
                etRemark.setText(data.getRemark());
                etRemark.addTextChangedListener(textWatcher);
            }
        }
        private ZMUINumberStepView.OnNumberChangeListener onNumberChangeListener=new ZMUINumberStepView.OnNumberChangeListener() {
            @Override
            public void onNumberChange(ZMUINumberStepView numberStepView, double number) {
                int pos= (int) numberStepView.getTag();
                Product itemData = getItemData(pos);
                itemData.setPrice(number);
            }
        };

        private ZMUITextWatcher textWatcher=new ZMUITextWatcher() {

            @Override
            public void afterTextChanged(View view, Editable text) {
                int pos = (int) view.getTag();
                String remark=text!=null?text.toString().trim():null;
                LogUtils.d(TAG, "afterTextChanged:"+remark);

                Product itemData = getItemData(pos);
                itemData.setRemark(remark);
            }
        };

    }



    private static class Product{
        private String name;
        private double price;
        private String remark;


        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }

}
